import 'package:flutter/material.dart';
import 'package:flutter_grammar/widget/myImage.dart';

class SwiperPage extends StatefulWidget {
  const SwiperPage({super.key});
  @override
  State<SwiperPage> createState() => _SwiperPageState();
}

class _SwiperPageState extends State<SwiperPage> {
  int _currentPageIndex = 0; // 指示器标记点
  List<Widget> list = [];

  @override
  void initState() {
    super.initState();
    list = [
      myImage(src: 'https://www.itying.com/images/flutter/1.png'),
      myImage(src: 'https://www.itying.com/images/flutter/2.png'),
      myImage(src: 'https://www.itying.com/images/flutter/3.png'),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('swiper')),
        body: Stack(
          children: [
            SizedBox(
              height: 200,
              child: PageView.builder(
                  onPageChanged: (int index) {
                    setState(() {
                      _currentPageIndex = index % (list.length);
                    });
                  },
                  itemCount: 1000,
                  itemBuilder: (context, index) {
                    return list[index % (list.length)]; // 简单的算法  已经无限轮播  其实是滑动1000次后 就会停止
                  }),
            ),
            Positioned(
              bottom: 10,
              left: 0,
              right: 0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: List.generate(list.length, (i) {
                  return Container(
                    margin: const EdgeInsets.fromLTRB(2, 0, 2, 0),
                    width: 10,
                    height: 10,
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color:
                            _currentPageIndex == i ? Colors.blue : Colors.grey),
                  );
                }).toList(),
              ),
            )
          ],
        ));
  }
}
